package com.rjpacket.supernews.app.task;

import android.os.AsyncTask;
import com.rjpacket.supernews.app.tasks.TaskCallOnBack;
import com.rjpacket.supernews.app.utils.HttpTool;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by WIN7 on 2015/3/15.
 */
public class DuanziListTask extends AsyncTask<String, Integer, Object> {
    private TaskCallOnBack back;

    public DuanziListTask(TaskCallOnBack back) {
        this.back = back;
    }

    @Override
    protected Object doInBackground(String... params) {
        Object ret = null;

        if (params != null && params.length > 0) {

            String url = params[0];

            byte[] bytes = HttpTool.get(url);

            if (bytes != null) {
                String str = new String(bytes);

                try {
                    ret = new JSONObject(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                //TODO 加载不了数据可以返回结束下拉
                return "error";
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(Object jsonObject) {
        if (jsonObject != null) {
            if (jsonObject instanceof JSONObject) {
                JSONObject json = (JSONObject) jsonObject;
                back.callOnBack(json);
            } else {
                back.callOnBack(jsonObject);
            }
        }
    }
}